<template>
  <el-card class="box-card">
    <!-- 1. 面包屑 -->
    <my-bread level1="商品管理" level2="商品列表"></my-bread>
    <!-- 2. 搜索 -->
    <el-row class="searchRow">
      <el-col>
        <el-input
          @clear="loadUserList()"
          placeholder="请输入内容"
          v-model="query"
          clearable
          class="inputSearch"
        >
          <!-- 搜索图标 -->
          <el-button @click="searchUser()" slot="append" icon="el-icon-search">
          </el-button>
        </el-input>
        <!-- 按钮 -->
        <el-button type="success" plain @click="$router.push({name:'goodsadd'})">添加商品</el-button>
      </el-col>
    </el-row>

    <!-- 3. 表格 -->
    <el-table :data="goodslist" style="width: 100%" stripe border>
      <el-table-column type="index" label="#" width="60"> </el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="600">
      </el-table-column>
      <el-table-column prop="goods_price" label="商品价格(元)" width="80"> </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量" width="80"> </el-table-column>

      <!-- <el-table-column prop="create_time | fmtdate" label="创建时间"> </el-table-column> -->
      <!-- <el-table-column prop="create_time" label="创建时间">{{create_time | fmtdate}}</el-table-column> -->
      <el-table-column label="创建日期">
        <!-- 如果单元格内提示的内容不是字符串（文本），
               需要给被现实的内容外层包裹一个template -->
        <!-- 
                template内部要用数据 设置slot-scope属性
                该属性的值是要用数据create_time的数据源userlist
             -->
        <!--  slot-scope的值userlist其实就是el-table绑定的数据userlist
                   userlist.row -> 数据中的每个对象

                   scope -> 自动找到上一级数据源userlist
              -->
        <template slot-scope="scope">
          {{ scope.row.create_time | fmtdate }}
        </template>
      </el-table-column>

      
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            size="mini"
            plain
            type="primary"
            icon="el-icon-edit"
            circle
            @click="showEditUserDia(scope.row)"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            size="mini"
            plain
            type="danger"
            icon="el-icon-delete"
            circle
            @click="showDeleUserMsgBox(scope.row.id)"
          ></el-button>
          <!-- 分配角色按钮 -->
          <el-button
            @click="showSetUserRoleDia(scope.row)"
            size="mini"
            plain
            type="success"
            icon="el-icon-check"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 4. 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="6"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 对话框 -->
    <!-- 1.添加用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisibleAdd">
      <el-form :model="form">
        <el-form-item label="用户名" label-width="100px">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" label-width="100px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" label-width="100px">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" label-width="100px">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="addUser()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 2.编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="dialogFormVisibleEdit">
      <el-form :model="form">
        <el-form-item label="用户名" label-width="100px">
          <el-input
            disabled
            v-model="form.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="邮箱" label-width="100px">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" label-width="100px">
          <el-input v-model="form.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="editUser()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 3.分配角色对话框 -->
    <el-dialog title="分配角色" :visible.sync="dialogFormVisibleRol">
      <el-form :model="form">
        <el-form-item label="用户名：" label-width="100px">
          {{currUsername}}
        </el-form-item>
        <el-form-item label="角色" label-width="100px">
          
          <!-- 如果select绑定的数据的值 和 option的value值一样 就会显示该option的label值 -->
          <el-select v-model="currRoleId">
            <el-option label="请选择" :value="-1"></el-option>
            <el-option :label="item.roleName" :value="item.id"
            v-for="(item,i) in roles"
            :key="i"
            ></el-option>
          </el-select>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRol = false">取 消</el-button>
        <el-button type="primary" @click="setRole()">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
export default {
  data() {
    return {
      query: "",

      //   表格绑定的数据
      // username: "admin"
      // email: "adsfad@qq.com"
      // mobile: "12345678"
      // create_time: 1486720211
      // mg_state: true
      // id: 500
      // role_name: "超级管理员"

      goodslist: [],
      //   分页相关的数据
      total: -1,
      pagenum: 1,
      pagesize: 6,
      // 添加用户对话框的属性
      dialogFormVisibleAdd: false,
      dialogFormVisibleEdit: false,
      dialogFormVisibleRol: false,
      // 添加用户的表单用户
      // username | 用户名称 | 不能为空 |
      // | password | 用户密码 | 不能为空 |
      // | email    | 邮箱     | 可以为空 |
      // | mobile   | 手机号   | 可以为空 |
      form: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      //分配角色
      currRoleId: 0,
      currUserId:-1,
      currUsername: '',
      // 保存所有的角色数据
      roles: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 分配角色 发送请求
    async setRole () {
       const res = await this.$http.put(`users/${this.currUserId}/role`,{
         rid:this.currRoleId
       })
      //  关闭对话框
       this.dialogFormVisibleRol = false
    },
    // 分配角色 打开对话框
    async showSetUserRoleDia(user) {
      this.currUsername = user.username
      // 给currUserId赋值
      this.currUserId = user.id
      // 获取所有的角色
         const res1 = await this.$http.get(`roles`)
         this.roles = res1.data.data
      // 获取当前用户的角色id - rid
      const res = await this.$http.get(`users/${user.id}`)
      // 获取当前用户的角色id rid   res.data.data.rid
       this.currRoleId = res.data.data.rid
      this.dialogFormVisibleRol = true
    },
    // 修改用户状态
    async changeMgState(user) {
      const res = await this.$http.put(
        `users/${user.id}/state/${user.mg_type}`
      );
    },
    // 编辑用户 发送请求
    async editUser() {
      const res = await this.$http.put(`users/${this.form.id}`, this.form);
      // console.log(res)
      // 1. 关闭对话框
      this.dialogFormVisibleEdit = false;
      // 2. 更新视图
      this.getUserList();
    },
    // 编辑用户 显示对话框
    showEditUserDia(user) {
      this.form = user;
      // 获取用户数据
      this.dialogFormVisibleEdit = true;
    },
    // 删除用户 打开消息盒子
    showDeleUserMsgBox(userId) {
      this.$confirm("删除用户？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 发送删除请求 id >>> 用户id
          // 1. data 中找 userId     x
          // 2. 吧userId 以showDeleUserMsgBox 参数形式传进来
          const res = await this.$http.delete(`users/${userId}`);
          console.log(res);
          if (res.data.meta.status === 200) {
            // 删除后回到第一页
            this.pagenum = 1;
            // 更新视图
            this.getUserList();
            // 提示
            this.$message({
              type: "success",
              message: res.data.meta.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 添加用户 发送请求
    async addUser() {
      // 2. 关闭对话框
      this.dialogFormVisibleAdd = false;

      const res = await this.$http.post(`users`, this.form);
      // console.log(res);
      const {
        meta: { status, msg },
        data,
      } = res.data;
      if (status === 200) {
        // 1. 提示成功
        this.$message.success(msg);
        // 3. 更新视图
        this.getUserList();
        // 4. 清空文本框
        // this.form = {} 最简单的写法
        // for in写法
        for (const key in this.form) {
          if (this.form.hasOwnProperty(key)) {
            this.form[key] = "";
          }
        }

        // 提示失败
      } else {
        this.$message.warning(msg);
      }
    },
    //获取用户列表的请求
    async getUserList() {
      //query  查询参数 可以为空
      //pagenum  当前页码  不能为空
      //pagesize  每页显示条数  不能为空

      //需要授权的 API ，必须在请求头中使用 `Authorization` 字段提供 `token` 令牌
      // 设置请求头 用到axios

      
      const res = await this.$http.get(
        `goods?query=${this.query}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
       
      );
//   
      const {
        meta: { status, msg },
        data: { goods, total },
      } = res.data;

      if (status === 200) {
        //   1.给表格数据赋值
        this.goodslist = goods;
        // 2. 给total赋值
        this.total = total;
        // 3. 提示成功
        // this.$message.success(msg);
      } else {
        //   提示失败
        // this.$message.warning(msg);
      }
    },
    // 分页用的
    handleSizeChange(val) {
      
      this.pagesize = val;
      this.pagenum = 1;
      this.getUserList();
    },
    handleCurrentChange(val) {
      
      this.pagenum = val;
      this.getUserList();
    },

    // 搜索用户
    searchUser() {
      this.getUserList();
    },
    //  清空搜索框时 重新获取数据
    loadUserList() {
      this.getUserList();
    },
    // 添加用户 显示对话框
    showAddUserDia() {
      this.form = {};
      this.dialogFormVisibleAdd = true;
    },
  },
};
</script>
<style>
.box-card {
  height: 100%;
}
.inputSearch {
  width: 400px;
}
.searchRow {
  margin-top: 20px;
}
</style>